-- Make GameData available in the loading screen (note: can only read, writing will not work properly)
-- Thanks to Hoeloe for making this work!
-- Originally made by MrDoubleA
local GameData
do
    package.path = package.path .. ";./scripts/?.lua"
    local serializer = require("ext/serializer")

    GameData = serializer.deserialize(Misc.GetRawGameData())
end

-- Code to make textplus/shaders work, also made by MrDoubleA
do
    local function exists(path)
        local f = io.open(path,"r")

        if f ~= nil then
            f:close()
            return true
        else
            return false
        end
    end

    Misc.resolveFile = (function(path)
        local inScriptPath = getSMBXPath().. "\\scripts\\".. path
        local inEpisodePath = _episodePath.. path

        return (exists(path) and path) or (exists(inEpisodePath) and inEpisodePath) or (exists(inScriptPath) and inScriptPath) or nil
    end)

    -- Make require work better
    local oldRequire = require

    function require(path)
        local inScriptPath = getSMBXPath().. "\\scripts\\".. path.. ".lua"
        local inScriptBasePath = getSMBXPath().. "\\scripts\\base\\".. path.. ".lua"
        local inEpisodePath = _episodePath.. path.. ".lua"

        local path = (exists(inEpisodePath) and inEpisodePath) or (exists(inScriptPath) and inScriptPath) or (exists(inScriptBasePath) and inScriptBasePath)
        assert(path ~= nil,"module '".. path.. "' not found.")

        return oldRequire(path)
    end

    -- lunatime
    _G.lunatime = require("engine/lunatime")

    -- Required stuff to actually get color to work
    local max = math.max;
	local min = math.min;
	local abs = math.abs;
    function math.clamp(a,mi,ma)
        mi = mi or 0;
        ma = ma or 1;
        return min(ma,max(mi,a));
    end
    function math.lerp(a,b,t)
        if type(a) == "Quaternion" and type(b) == "Quaternion" and a.__nrm and b.__nrm then
            local q = a*(1-t) + b*t
            q:normalise()
            q.__nrm = true
            return q
        else
            return a*(1-t) + b*t
        end
    end

    -- Color
    _G.Color = require("engine/color")
end

local loadScreenPath = mem(0x00B2C61C, FIELD_STRING) .. "GFX/"
local loadScreenPath2 = mem(0x00B2C61C, FIELD_STRING) .. "Fonts/"

textplus = require("Libraries/textplusOLD")

GoodFont = textplus.loadFont(loadScreenPath2 .. "Enjl/6.ini")

local HardModeGFX = Graphics.loadImage(loadScreenPath .. "hard mode high incomplete.png")
local Logo
local Opacity = 0
local XTime = -130

local RealLevelName = readmem(0xB25720, FIELD_STRING)
local LevelsTable = GameData.LevelsTable

local function SearchLevelName(level)
    for key, value in pairs(LevelsTable) do
        if LevelsTable[key].level == level then
            return LevelsTable[key]
        end
    end
end

function onDraw()
    LevelsTable = GameData.LevelsTable
    Opacity = Opacity + 0.018
    XTime = XTime + 6
    if XTime > 800 + 130 then
        XTime = -130
    end

    local LevelName = ""
    local WorldNumber = 0
    if SearchLevelName(RealLevelName) then -- done specifically because the hub world errors out, and i have no fucking clue why
        LevelName = SearchLevelName(RealLevelName).name
        WorldNumber = SearchLevelName(RealLevelName).world
    end

    if not Logo then
        Logo  = Graphics.loadImage(loadScreenPath .. "unusuality logo " .. WorldNumber .. ".png")
    end

    if GameData.HardMode then
        if GameData.HMLevelsBeat and GameData.HMLevelsBeat[RealLevelName] then
            HardModeGFX = Graphics.loadImage(loadScreenPath .. "hard mode high.png")
        end
        Graphics.drawImage(HardModeGFX, (800/2) - (HardModeGFX.width/2), (600/2) - (HardModeGFX.height/2) - 60, Opacity)
    end
    Graphics.drawImage(Logo, (800/2) - (Logo.width/2), (600/2) - (Logo.height/2), Opacity)

    textplus.print{
        x = XTime,
        y = 500,
        text = "<size 2><wave 5>".. "Loading!" .. "</wave></size>",
        font = GoodFont,
        color = Color.white*Opacity
    }

    textplus.print{
        x = 400,
        y = 350,
        pivot = {0.5, 0.5},
        text = "<size 2>".. LevelName .. "</size>",
        font = GoodFont,
        color = Color.white*Opacity
    }
end